package com.adcc.loadsheet.billing.service.impl;

import com.adcc.loadsheet.billing.entity.SystemErrorLogEntity;
import com.adcc.loadsheet.billing.entity.SystemJobLogEntity;
import com.adcc.loadsheet.billing.entity.SystemOperationLogEntity;
import com.adcc.loadsheet.billing.mapper.mysql.SystemErrorLogEntityMapper;
import com.adcc.loadsheet.billing.mapper.mysql.SystemJobLogEntityMapper;
import com.adcc.loadsheet.billing.mapper.mysql.SystemOperationLogEntityMapper;
import com.adcc.loadsheet.billing.service.LogService;
import com.adcc.loadsheet.billing.util.DateTimeUtil;
import com.adcc.loadsheet.billing.vo.SystemErrorLogEntityVO;
import com.adcc.loadsheet.billing.vo.SystemJobLogEntityVO;
import com.adcc.loadsheet.billing.vo.SystemOperationLogEntityVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 异常日志service实现类
 */
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    SystemOperationLogEntityMapper systemOperationLogEntityMapper;

    @Autowired
    SystemErrorLogEntityMapper systemErrorLogEntityMapper;

    @Autowired
    SystemJobLogEntityMapper systemJobLogEntityMapper;

    //新增系统操作日志
    @Override
    public void operationLog(Integer operationType, String content, String operator) {
        SystemOperationLogEntity entity = new SystemOperationLogEntity();
        entity.setOperator(operator);
        entity.setOperationType(operationType);
        entity.setContent(content);
        entity.setCreateAt(DateTimeUtil.getNowDate());
        systemOperationLogEntityMapper.insertOperationLog(entity);
    }

    //查询系统操作日志
    @Override
    public PageInfo<SystemOperationLogEntityVO> selectSystemOperationLogForPage(Map<String, Object> map){
        Integer pageNum = Integer.valueOf(map.get("pageNum").toString());
        Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize);
        Page<SystemOperationLogEntity> entityPage = (Page<SystemOperationLogEntity>) systemOperationLogEntityMapper.select(map);
        Page<SystemOperationLogEntityVO> voPage = new Page<>(pageNum, pageSize);
        voPage.setPages(entityPage.getPages());
        voPage.setTotal(entityPage.getTotal());
        entityPage.stream().forEach(entity -> voPage.add(entity.toVO()));
        return new PageInfo<>(voPage);
    }

    //新增系统异常日志
    @Override
    public void errorLog(Integer logType, String content) {
        SystemErrorLogEntity entity = new SystemErrorLogEntity();
        entity.setLogType(logType);
        entity.setContent(content);
        entity.setCreateAt(DateTimeUtil.getNowDate());
        systemErrorLogEntityMapper.insertErrorLog(entity);
    }

    //查询系统异常日志
    @Override
    public PageInfo<SystemErrorLogEntityVO> selectSystemErrorLogForPage(Map<String, Object> map){
        Integer pageNum = Integer.valueOf(map.get("pageNum").toString());
        Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize);
        Page<SystemErrorLogEntity> entityPage = (Page<SystemErrorLogEntity>) systemErrorLogEntityMapper.select(map);
        Page<SystemErrorLogEntityVO> voPage = new Page<>(pageNum, pageSize);
        voPage.setPages(entityPage.getPages());
        voPage.setTotal(entityPage.getTotal());
        entityPage.stream().forEach(entity -> voPage.add(entity.toVO()));
        return new PageInfo<>(voPage);
    }

    //新增JOB日志
    @Override
    public void jobLog(Integer logType, String content) {
        SystemJobLogEntity entity = new SystemJobLogEntity();
        entity.setLogType(logType);
        entity.setContent(content);
        entity.setCreateAt(DateTimeUtil.getNowDate());
        systemJobLogEntityMapper.insertJobLog(entity);
    }

    //查询JOB日志
    @Override
    public PageInfo<SystemJobLogEntityVO> selectSystemJobLogForPage(Map<String, Object> map){
        Integer pageNum = Integer.valueOf(map.get("pageNum").toString());
        Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize);
        Page<SystemJobLogEntity> entityPage = (Page<SystemJobLogEntity>) systemJobLogEntityMapper.select(map);
        Page<SystemJobLogEntityVO> voPage = new Page<>(pageNum, pageSize);
        voPage.setPages(entityPage.getPages());
        voPage.setTotal(entityPage.getTotal());
        entityPage.stream().forEach(entity -> voPage.add(entity.toVO()));
        return new PageInfo<>(voPage);
    }
}